/* $Id: SubmissionsFormatter.java 116 2011-08-22 13:43:59Z altdotua@gmail.com $ */

package alt.djudge.frontend.server.gui;

import java.util.Map;

import alt.djudge.frontend.server.datatypes.ContestEntry;
import alt.djudge.frontend.server.datatypes.ContestProblemEntry;
import alt.djudge.frontend.server.datatypes.LanguageEntry;
import alt.djudge.frontend.server.datatypes.SubmissionEntry;
import alt.djudge.frontend.server.datatypes.UserEntry;
import alt.djudge.frontend.server.models.ContestProblemsModel;
import alt.djudge.frontend.server.models.LanguagesModel;
import alt.djudge.frontend.server.models.UsersModel;
import alt.djudge.frontend.server.utils.Linker;


public class SubmissionsFormatter extends AbstractFormatter<SubmissionEntry>
{
	private SubmissionsDataFormatSettings df = new SubmissionsDataFormatSettings();

	@SuppressWarnings("unused")
	private SubmissionsStyleFormatSettings sf = new SubmissionsStyleFormatSettings();
	
	private Map<Long, LanguageEntry> languages = LanguagesModel.getEntriesMap();
	
	private Map<Long, ContestProblemEntry> problems = ContestProblemsModel.getEntriesMap();
	
	private Map<Long, UserEntry> users = UsersModel.getEntriesMap();
	
	private String trClass = "b1";
	
	private String tdClass = "b1";
	
	private String thClass = "b1";

	public SubmissionsFormatter()
	{
		
	}
	
	public SubmissionsFormatter(ContestEntry c)
	{
		SubmissionsDataFormatSettings d = new SubmissionsDataFormatSettings(c);
		this.setDataFormatSettings(d);
	}
	
	public SubmissionsFormatter(SubmissionsDataFormatSettings dataFormatSettings)
	{
		this.setDataFormatSettings(dataFormatSettings);
	}

	public SubmissionsFormatter(SubmissionsDataFormatSettings dataFormat, SubmissionsStyleFormatSettings styleFormat)
	{
		this.sf = styleFormat;
		this.setDataFormatSettings(dataFormat);
	}

	@Override
	protected void addClassData(SubmissionEntry entry)
	{
		SubmissionEntry s = (SubmissionEntry) entry;
		
		if (getDataFormatSettings().showSubmissionId)
			addTableData(s.getId());
		if (getDataFormatSettings().showContestTime)
			addTableData(formatContestTime(s.getContestTime()));
		if (getDataFormatSettings().showSubmissionTime)
			addTableData(formatDate(s.getSubmissionTime()));
		if (getDataFormatSettings().showProblemName)
			addTableData(problems.get(s.getContestProblemId()).getSid());
		if (getDataFormatSettings().showLanguageName)
			addTableData(languages.get(s.getLanguageId()).getSid());
		if (getDataFormatSettings().showUserName)
			addTableData(users.get(s.getUserId()) != null ? users.get(s.getUserId()).getUsername() : "n/a");
		if (getDataFormatSettings().showSorceSize)
		{
			String link = Linker.link("olymp2011/source.jsp?submission_id=" + s.getId());
			addTableData(formatSourceSize(s.getSourceSize()), link);
		}
		if (getDataFormatSettings().showJudgement)
		{
			String link = Linker.link("olymp2011/submission.jsp?submission_id=" + s.getId());
			addTableData(formatJudgement(s.getJudgement()), link);
		}
		if (getDataFormatSettings().showScore)
			addTableData(formatScore(s.getScore()));
		if (getDataFormatSettings().showWrongTestNumber)
			addTableData(formatScore(s.getWrongTestNumber()));
		if (getDataFormatSettings().showMaxTime)
			addTableData(formatRuntime(s.getMaxTimeUsed()));
		if (getDataFormatSettings().showMaxMemory)
			addTableData(formatMemory(s.getMaxMemoryUsed()));
		if (getDataFormatSettings().showState)
			addTableData(formatState(s.getDjudgeFlag()));
		if (getDataFormatSettings().showIP)
			addTableData(s.getIpAddress());		
		/*
			<td><a href="<%= Linker.link("source.jsp?submission_id=" + submission.getId()) %>"><%= submission.getSourceSize() %></a></td>
			<td><a href="<%= Linker.link("submission.jsp?submission_id=" + submission.getId()) %>"><%= submission.getJudgement() %></a></td>
			<td><a href="<%= Linker.link("submission.jsp?submission_id=" + submission.getId()) %>"><%= submission.getScore() %></a></td>
			<td><a href="<%= Linker.link("admin/submissiondetails.jsp?submissiondetails_id=" + submission.getSubmissionDetailsId()) %>"><%= submission.getSubmissionDetailsId() %></a></td>
			<td><a href="<%= Linker.link("admin/submission.jsp?submission_id=" + submission.getId()) %>">Edit</a></td>
			<td><a href="<%= Linker.link("submissions?action=disable&id=" + submission.getId()) %>">Disable</a></td>
			<td><a href="<%= Linker.link("submissions?action=rejudge&id=" + submission.getId() + "&return=" + requestUrlEncoded) %>">Rejudge</a></td>
			<td><a href="<%= Linker.link("submissions?action=delete&id=" +  submission.getId()) %>">Delete</a></td>

		 */
	}

	@Override
	protected void addClassHeaders()
	{
		if (getDataFormatSettings().showSubmissionId)
			addTableHeader("ID", "st_team");
		if (getDataFormatSettings().showContestTime)
			addTableHeader("Час", "st_team");
		if (getDataFormatSettings().showSubmissionTime)
			addTableHeader("Час", "st_team");
		if (getDataFormatSettings().showProblemName)
			addTableHeader("Задача", "st_team");
		if (getDataFormatSettings().showLanguageName)
			addTableHeader("Мова", "st_team");
		if (getDataFormatSettings().showUserName)
			addTableHeader("User", "st_team");
		if (getDataFormatSettings().showSorceSize)
			addTableHeader("Код", "st_team");
		if (getDataFormatSettings().showJudgement)
			addTableHeader("Результат", "st_team");
		if (getDataFormatSettings().showScore)
			addTableHeader("Бали", "st_team");
		if (getDataFormatSettings().showWrongTestNumber)
			addTableHeader("Тест", "st_team");
		if (getDataFormatSettings().showMaxTime)
			addTableHeader("Час в-ня", "st_team");
		if (getDataFormatSettings().showMaxMemory)
			addTableHeader("Память", "st_team");
		if (getDataFormatSettings().showState)
			addTableHeader("Стан", "st_team");
		if (getDataFormatSettings().showIP)
			addTableHeader("IP", "st_team");
	}

	@Override
	protected String getCustomRowClass(SubmissionEntry entry)
	{
		String color = null;
		if (entry.isCompilationError() || entry.getFlagFirstTestOnly())
			color = colorCE;
		else if (entry.isPending())
			color = colorPending;
		else if (entry.isAccepted())
			color = colorAC;
		else if (entry.isWrongAnswer())
			color = colorWA;
		else  if (entry.isTimeLimitExceeded())
			color = colorTLE;
		else if (entry.isMemoryLimitExceeded())
			color = colorMLE;
		else if (entry.isRuntimeError())
			color = colorRE;
		
		if (color != null)
			return "background-color: " + color + "; ";
		
		// unknown judgment
		return "background-color: #777777; ";
	}
	
	protected String getTrHtmlClass()
	{
		return trClass;
	}
	
	protected String getTdHtmlClass()
	{
		return tdClass;
	}
	
	protected String getThHtmlClass()
	{
		return thClass;
	}

	public void setDataFormatSettings(SubmissionsDataFormatSettings df)
	{
		this.df = df;
	}

	public SubmissionsDataFormatSettings getDataFormatSettings()
	{
		return df;
	}
}
